package com.cd.university.common;

import com.google.common.hash.Funnels;
import org.springframework.stereotype.Component;

/**
 * 布隆过滤器，防止缓存穿透
 * @author 陈栋
 * @create 2021/11/18 12:05
 */
@Component
public class BloomFilter {
    // 设置比特位的数组长度为一百万
    private static final Integer SIZE = 1000000;

    private static final double fpp = 0.0001;

    private static com.google.common.hash.BloomFilter<Integer> bloomFilter = null;

    public void bloomFilter() {
        createBloomFilter(SIZE);
    }

    public void bloomFilter(Integer size) {
        createBloomFilter(size);
    }

    private void createBloomFilter(Integer size) {
        bloomFilter = com.google.common.hash.BloomFilter.create(Funnels.integerFunnel(), SIZE, fpp);
        for (int i = 1; i <= size; i++) {
            bloomFilter.put(i);
        }
    }
}
